<template>
  <div class="login_cont">
    <div class="loginBox">
      <!-- 头像区域 -->
      <div class="img">
        <img src="../assets/logo.png" alt srcset />
      </div>
      <!-- 登陆表单区域 -->
      <el-form
        label-width="0px"
        class="login_form"
        :model="logform"
        :rules="logrules"
        ref="loginFormRef"
      >
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="logform.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="logform.password"
            type="password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登陆</el-button>
          <el-button type="info" @click="resetlogForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      logform: {
        username: '',
        password: ''
      },
      logrules: {
        username: [
          { required: true, message: '账号不能为空', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetlogForm () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async result => {
        if (result === false) {
          // 校验失败 提示用户
          this.$message.error('账号或密码格式有误')
        } else {
          // 校验成功,发送登陆请求
          const { data: res } = await this.$http.post('login', this.logform)
          if (res.meta.status !== 200) {
            this.$message.error('登陆失败:' + res.meta.msg)``
          } else {
            // 登陆成功
            this.$message.success('登陆成功')
            // 保存服务器返回过来的token
            window.sessionStorage.setItem('token', res.data.token)
            // 跳转到后台页面
            this.$router.push('/home')
          }
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login_cont {
  height: 100%;
  background-color: #2b4c6b;
}
.loginBox {
  position: absolute;
  width: 450px;
  height: 300px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  border-radius: 3px;
  .img {
    position: absolute;
    top: -75px;
    left: 50%;
    transform: translate(-50%, 0);
    width: 130px;
    height: 130px;
    border-radius: 50%;
    border: 1px solid #eee;
    box-shadow: 0 0 10px #ddd;
    padding: 10px;
    background-color: #fff;
    img {
      width: 100%;
      width: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .login_form {
    position: absolute;
    bottom: 0;
    left: 0;
    padding: 0 20px;
    width: 100%;
    box-sizing: border-box;
  }
  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>